package axb;

import java.sql.ResultSet;

public class crud {

    public comm comm;

    public crud() throws Exception {
        comm = new comm();
    }

    public void create(book book) throws Exception {
        String sql = String.format("insert into book(bookname,author,price,remark) values('%s','%s',%s,'%s')",
                book.getBookname(), book.getAuthor(), book.getPrice(), book.getRemark());

        comm.Exec(sql);
        System.out.println("添加执行完成");
    }

    public void update(book book) throws Exception {
        String sql = "update book";
        if (book.getBookname() != "") {
            sql += String.format(" set bookname='%s'", book.getBookname());
        }
        if (book.getAuthor() != "") {
            sql += String.format(",author='%s'", book.getAuthor());
        }
        if (book.getRemark() != "") {
            sql += String.format(",remark='%s'", book.getRemark());
        }
        if (!Double.isNaN(book.getPrice())) {
            sql += String.format(",price='%s'", book.getPrice());
        }
        sql+=String.format(" where id=%s", book.getId());
        comm.Exec(sql);
        System.out.println("更新执行完成");
    }

    public void delete(int id) throws Exception {
        String sql = String.format("delete from book where id=%s", id);
        comm.Exec(sql);
        System.out.println("删除执行完成");
    }

    public ResultSet QueryAll() throws Exception {
        String sql = "select * from book";
        ResultSet rs = comm.Query(sql);
        return rs;
    }

    public void create(String bookname, String author, Double price, String remark) throws Exception {
        String sql = String.format("insert into book(bookname,author,price,remark) values('%s','%s',%s,'%s')", bookname,
                author, price, remark);
        comm.Exec(sql);
        System.out.println("添加执行完成");
    }

    public String exec(String sql, String optionName) throws Exception {
        comm.Exec(sql);
        return String.format("%s 执行完成", optionName);
    }

    public ResultSet query(String sql) throws Exception {
        ResultSet rs = comm.Query(sql);
        return rs;
    }
}
